package com.wayneleo.launcher.core;

import java.io.File;

public class LauncherContext {
    /*-
     * 应用根目录
     */
    private final String homeDir;
    /*-
     * 启动,停止,系统配置等其它非业务脚本以及配置
     */
    private final String binDir;
    /*-
     * 业务配置文件
     */
    private final String confDir;
    /*-
     * 非业务的jar包,例如框架或其它被依赖的jar包
     */
    private final String frameworkDir;
    /*-
     * 业务模块的jar包
     */
    private final String modulesDir;
    /*-
     * 模块配置文件的名称
     */
    private final String moduleConfFilename;
    /*-
     * 持有一个自己的对象(单例模式)
     */
    private static LauncherContext selfInstance;

    private LauncherContext(final String homeDir, final String moduleConfFilename) {
        this.homeDir = homeDir;
        this.binDir = homeDir + File.separator + "bin";
        this.confDir = homeDir + File.separator + "conf";
        this.frameworkDir = homeDir + File.separator + "framework";
        this.modulesDir = homeDir + File.separator + "modules";
        this.moduleConfFilename = moduleConfFilename;
    }

    public static LauncherContext getInstance() {
        return selfInstance;
    }

    protected static LauncherContext init(final String homeDir, final String moduleConfFilename) {
        if (null == selfInstance) {
            syncInit(homeDir, moduleConfFilename);
        }
        return selfInstance;
    }

    private static synchronized LauncherContext syncInit(final String homeDir, final String moduleConfFilename) {
        if (null == selfInstance) {
            selfInstance = new LauncherContext(homeDir, moduleConfFilename);
        }
        return selfInstance;
    }

    public String getHomeDir() {
        return homeDir;
    }

    public String getBinDir() {
        return binDir;
    }

    public String getConfDir() {
        return confDir;
    }

    public String getFrameworkDir() {
        return frameworkDir;
    }

    public String getModulesDir() {
        return modulesDir;
    }

    public String getModuleConfFilename() {
        return moduleConfFilename;
    }
}
